package com.xiaoming.backend.controller;

import com.xiaoming.backend.dto.request.FindOrderRequest;
import com.xiaoming.backend.dto.request.UptOrderRequest;
import com.xiaoming.backend.dto.request.WasteRegistrationRequest;
import com.xiaoming.backend.dto.response.OrderResponse;
import com.xiaoming.backend.entity.orm.User;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.service.IWasteService;
import com.xiaoming.backend.util.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/waste")
@RequiredArgsConstructor
@PreAuthorize("@securityService.checkInstitution(#institutionId)")
@Tag(name = "WasteController", description = "订单相关接口")
public class WasteController {
    private final IWasteService wasteService;

    @PostMapping("/register")
    @Operation(summary = "registerWaste", description = "登记订单")
    public ResponseEntity<?> registerWaste(@RequestBody WasteRegistrationRequest wasteRegistrationRequest) {
        try {
            // 从认证信息中提取 User 对象（需类型转换）
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            wasteService.registerWaste(user,wasteRegistrationRequest);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok("添加订单成功");
    }

    @PostMapping("/find")
    @Operation(summary = "findOrder", description = "按条件查找订单")
    public ResponseEntity<?> findOrder(@RequestBody FindOrderRequest findOrderRequest) {
        String jsonStr = "";
        try {
            List<OrderResponse> orders = wasteService.findOrder(findOrderRequest);
            jsonStr = JsonUtil.objToJson(orders);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }

    @PostMapping("/upt")
    @Operation(summary = "uptOrder", description = "更新订单状态")
    public ResponseEntity<?> uptOrder(@RequestBody UptOrderRequest uptOrderRequest) {
        String jsonStr = "";
        try {
            // 从认证信息中提取 User 对象（需类型转换）
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            int i = wasteService.uptOrder(user,uptOrderRequest);
            jsonStr = JsonUtil.objToJson(i);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }

}

